#include "StoneKnight.h"
#include "Resource.h"


StoneKnight::StoneKnight(LPDIRECT3DDEVICE9 d3ddv,  LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State, int Direction): Object(x, y)//, ID, Object_State)
{
	this->d3ddv = d3ddv;
	this->BackBuffer = BackBuffer;
	this->SpriteHandler = SpriteHandler;

	this->setIDObjType(ID);

	this->blood = 2;

	is_turn_left = false;

	pos_x = x;
	pos_y = y;
	 
	pos_x_old = x;
	pos_y_old = y;
	center_pos_x = x;
	
	veloc_x = 0;
	veloc_y = 0;
	accel_y = 0;

	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[48]->getFileName()), 125, 0);
	p_light = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[7]->getFileName()), 125, 0);

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	timeDie = 0;

	is_gravity = true;
	
	this->obj_state = Object_State;//ES_ACTIVING;
	direction = Direction;

	if (obj_state == ES_NORMAL)
	{
		if (direction == 1)			//sang trai
		{
			turnLeft();
			/*is_turn_left = true;
			p_sprite->setFrame(0,2);
			p_sprite->setIndex(0);*/
		} 
		else if (direction == 2)	//sang phai
		{
			turnRight();
			/*is_turn_left = false;
			p_sprite->setFrame(3,5);
			p_sprite->setIndex(3);*/
		}
	} 
	else if (obj_state == ES_ACTIVING)
	{
		if (direction == 1)			//sang trai
		{
			turnLeft();			
			
		} 
		else if (direction == 2)	//sang phai
		{
			turnRight();
			
		}
		changeState(ES_ACTIVING);
	}
	
}

StoneKnight::~StoneKnight()
{
	/*if (_sprite != NULL)
	delete _sprite;*/
	
}

 

void StoneKnight::render(int cameraX, int cameraY)
{
	if (obj_state != ES_REMOVE && obj_state != ES_NORMAL)
	{
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}
}

OBJECT_TYPE StoneKnight::getObjectType()
{
	return OBJECT_ENEMY;
}

/**--------------------------------------------------------
*Description : Turn left Turtle
*Method name : TurnLeft
*Parameters :   
*Return value : void
--------------------------------------------------------**/
void StoneKnight :: turnLeft()
{
	is_turn_left = true;
	p_sprite->setFrame(0, 3);
	p_sprite->setIndex(0);
	veloc_x = (float)-STONE_KNIGHT_VX;
}


/**--------------------------------------------------------
*Description : Turn right Turtle
*Method name : TurnRight
*Parameters :   
*Return value : void
--------------------------------------------------------**/
void StoneKnight :: turnRight()
{
	is_turn_left = false;
	p_sprite->setFrame(3, 6);
	p_sprite->setIndex(3);
	veloc_x = (float)STONE_KNIGHT_VX;
}

void StoneKnight::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	//switch (obj_state)
	//{
	//case ES_ACTIVING:
	//	move(obj_list, gameTime);//Move(staObjs, gameTime);
	//	
	//	if (pos_y <= 0)	//goomba roi xuong ho, height = 0
	//	{
	//		changeState(ES_REMOVE);
	//	}
	//	break;

	//case ES_DIED:
	//	timeDie += gameTime;
	//	if (timeDie > 900)
	//	{
	//		changeState(ES_REMOVE);
	//	}
	//	break;

	//case ES_FALL:
	//	pos_x += veloc_x * gameTime;
	//	veloc_y -= accel_y * gameTime;
	//	pos_y += veloc_y * gameTime + 1/2 * accel_y * gameTime *gameTime;
	//	if (pos_y <= 0)	//goomba roi xuong ho, height = 0
	//	{
	//		changeState(ES_REMOVE);
	//	}
	//	
	//	break;
	//}
	//
	//p_sprite->update(gameTime);
	
}

void StoneKnight::update(list<Object*>* obj_list, DxInput* input, float gameTime, Object* simon)
{
	switch (obj_state)
	{
	case ES_NORMAL:
		{
			if (simon->getPosX() + 200 > this->pos_x) //tinh khoang cach de active dog  enemy
			{
				changeState(ES_ACTIVING);
			}
		}
		break;

	case ES_JUMP:
		{
			width = this->getSprite()->getTexture()->getWidth();
			height = this->getSprite()->getTexture()->getHeight();
			

			//Xet va cham theo truc Y
			if (is_gravity == false)
			{
				accel_y = 0;
			}
			else
			{
				accel_y = STONE_KNIGHT_GRAVITY;
			}
			veloc_y -= accel_y * gameTime;
			pos_y += veloc_y * gameTime + 1/2.0 * accel_y * gameTime * gameTime;

			Object* obj;
			list<Object*>::iterator i_obj;
			for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
			{
				obj = *i_obj;
				if (obj->getObjectType() == OBJECT_LAND)
				{
					Box box(this->pos_x_old,
						this->pos_y_old - this->height,
						this->width,
						this->height,
						0,
						veloc_y * gameTime + 1/2.0 * accel_y * gameTime * gameTime);

					Box block(obj->getPosX(),
						obj->getPosY() - obj->getHeight(),
						obj->getWidth(),
						obj->getHeight());
					 
					Box boxphase = Collision::GetSweptBroadphaseBox(box);
					if (Collision::AABBCheck(boxphase, block))
					{
						float normalx = 0;
						float normaly = 0;
						float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
						if (0 < collision_time && collision_time < 1)
						{
							is_gravity = false;
							if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
							{
								/*if (this->pos_y_old - this->height < obj->getPosY())
								{
									pos_x = pos_x_old + veloc_x * collision_time;
									veloc_x = -veloc_x;						
								}*/
							} 
							else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
							{
								/*if (this->pos_y_old - this->height < obj->getPosY())
								{
									pos_x = pos_x_old + veloc_x * collision_time;
									veloc_x = -veloc_x;						
								}*/
							}
							else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
							{

							}
							else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
							{
								pos_y = pos_y_old + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time;
								accel_y = 0;
								veloc_y = 0;

								changeState(ES_ACTIVING);
							}//end if

						}
						else
						{
							is_gravity = true;
						}//end if
					}
					else
					{
						is_gravity = true;
					}//end if
				}
				else
				{
					is_gravity = true;
				}//end if
			}//end for

			pos_x_old = pos_x;
			pos_y_old = pos_y;
		}
		break;

	case ES_ACTIVING:
		move(obj_list, gameTime, simon);
		if (pos_y <= 0)	//goomba roi xuong ho, height = 0
		{
			changeState(ES_REMOVE);
		}
		break;

	case ES_PAUSE:
		timeDie += gameTime;
		if (timeDie > 500)
		{
			changeState(ES_ACTIVING);
		}
		break;
 
	case ES_DIED:
		timeDie += gameTime;
		if (timeDie > 900)
		{
			changeState(ES_REMOVE);
		}
		break;

	default:
		break;
	}

	p_sprite->update(gameTime);

}

void StoneKnight::move(list<Object*>* obj_list, float TPF)
{
	//Object* obj;
	//list<Object*>::iterator i;
	//width = p_sprite->getTexture()->getWidth();
	//height = p_sprite->getTexture()->getHeight();
	//
	//pos_x_old = pos_x;
	//pos_y_old = pos_y;

	//pos_x_old = pos_x;
	//pos_y_old = pos_y;
	//

	////Xet va cham theo truc X
	//pos_x += veloc_x * TPF;
	//
	//

	////Xet va cham theo truc Y
	//if (is_gravity == false)
	//{
	//	accel_y = 0;
	//}
	//else
	//{
	//	accel_y = RED_DRAGON_GRAVITY;
	//}	
	//veloc_y -= accel_y * TPF;
	//pos_y += veloc_y * TPF + 1/2.0 * accel_y * TPF * TPF;
	////ResetRect();


	////kiem tra va cham nam tren gach, da
	//for (i = obj_list->begin(); i != obj_list->end(); i++)
	//{
	//	obj = *i;
	//	if (obj->getObjectType() == OBJECT_LAND)
	//	{
	//		//Box box = this->getBox(time);
	//		Box box(this->pos_x_old,
	//			this->pos_y_old - this->height, 
	//			width, 
	//			height, 
	//			veloc_x * TPF, 
	//			veloc_y * TPF + 1.0f/2.0f * accel_y * TPF * TPF);

	//		//Box block = obj->getBox(time);
	//		Box block(obj->getPosX(),
	//			obj->getPosY() - obj->getHeight(),// - 0.11,
	//			obj->getWidth(),
	//			obj->getHeight()
	//			);

	//		Box boxphase = Collision::GetSweptBroadphaseBox(box);
	//		if (Collision::AABBCheck(boxphase, block))
	//		{
	//			float normalx = 0;
	//			float normaly = 0;
	//			float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
	//			if (0 < collision_time && collision_time < 1)
	//			{
	//				is_gravity = false;
	//				if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
	//				{
	//					//pos_x = _xOld + veloc_x * collision_time;// + 1.0f/2.0f * accel_x * collision_time *collision_time;
	//					//accel_x = 0;
	//					//veloc_x = -veloc_x;

	//					if (this->pos_y_old - this->height < obj->getPosY())
	//					{
	//						pos_x = pos_x_old + veloc_x * collision_time;
	//						veloc_x = -veloc_x;						
	//					}
	//				} 
	//				else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
	//				{
	//					//pos_x = _xOld + veloc_x * collision_time;// + 1.0f/2.0f * accel_x * collision_time *collision_time;
	//					//accel_x = 0;
	//					//veloc_x = -veloc_x;

	//					if (this->pos_y_old - this->height < obj->getPosY())
	//					{
	//						pos_x = pos_x_old + veloc_x * collision_time;
	//						veloc_x = -veloc_x;						
	//					}
	//				}
	//				else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
	//				{
	//					
	//				}
	//				else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
	//				{
	//					

	//					pos_y = pos_y_old + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time;// + 0.5;
	//					accel_y = 0;
	//					veloc_y = 0;

	//					
	//				}

	//				/*if (this->_yOld - this->height <= obj->getPosY() + 1.5)
	//				{
	//					isGravity = false;
	//				}
	//				else
	//				{
	//					isGravity = true;
	//				}*/
	//				 

	//				

	//				 
	//			}
	//			else
	//			{
	//				
	//				is_gravity = true;
	//			}
	//		}
	//		else
	//		{
	//			is_gravity = true;
	//			
	//		}


	//		 
	//	}
	//	else
	//	{
	//		is_gravity = true;
	//	}
	//}

	//if(veloc_x > 0)
	//{
	//	turnRight();
	//}
	//else
	//{
	//	turnLeft();
	//}

	//pos_x_old = pos_x;
	//pos_y_old = pos_y;
	//pos_x_old = pos_x;
	//pos_y_old = pos_y;
	//	

	//if (veloc_x >= 0)
	//{
	//	direction = 2;
	//} 
	//else
	//{
	//	direction = 1;
	//}
}

void StoneKnight::move(list<Object*>* obj_list, float TPF, Object* simon)
{
	Object* obj;
	list<Object*>::iterator i;
	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();
	
	pos_x_old = pos_x;
	pos_y_old = pos_y;

	

	//Xet va cham theo truc X
	pos_x += veloc_x * TPF;
	
	

	//Xet va cham theo truc Y
	if (is_gravity == false)
	{
		accel_y = 0;
	}
	else
	{
		accel_y = STONE_KNIGHT_GRAVITY;
	}	
	veloc_y -= accel_y * TPF;
	pos_y += veloc_y * TPF + 1/2.0 * accel_y * TPF * TPF;
	


	//kiem tra va cham nam tren gach, da
	for (i = obj_list->begin(); i != obj_list->end(); i++)
	{
		obj = *i;
		if (obj->getObjectType() == OBJECT_LAND)
		{
			
			Box box(this->pos_x_old,
				this->pos_y_old - this->height, 
				width, 
				height, 
				veloc_x * TPF, 
				veloc_y * TPF + 1.0f/2.0f * accel_y * TPF * TPF);
			
			Box block(obj->getPosX(),
				obj->getPosY() - obj->getHeight(),
				obj->getWidth(),
				obj->getHeight());

			Box boxphase = Collision::GetSweptBroadphaseBox(box);
			if (Collision::AABBCheck(boxphase, block))
			{
				float normalx = 0;
				float normaly = 0;
				float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
				if (0 < collision_time && collision_time < 1)
				{
					is_gravity = false;
					if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
					{
						if (this->pos_y_old - this->height < obj->getPosY())
						{
							pos_x = pos_x_old + veloc_x * collision_time;
							turnRight();
						}
					} 
					else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
					{
						if (this->pos_y_old - this->height < obj->getPosY())
						{
							pos_x = pos_x_old + veloc_x * collision_time;
							turnLeft();
						}
					}
					else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
					{
						
					}
					else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
					{
						pos_y = pos_y_old + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time;
						accel_y = 0;
						veloc_y = 0;
					}//end if
					 
				}
				else
				{
					is_gravity = true;
				}//end if
			}
			else
			{
				is_gravity = true;
			}//end if			 
		}
		else
		{
			is_gravity = true;
		}//end if
	}//end for



	//va cham voi simon
	//Box box(this->pos_x_old,
	//	this->pos_y_old - this->height, 
	//	width, 
	//	height, 
	//	veloc_x * TPF, 
	//	veloc_y * TPF + 1.0f/2.0f * accel_y * TPF * TPF);


	//Box block(simon->getPosX(),
	//	simon->getPosY() - simon->getHeight(),
	//	simon->getWidth() - (60 - 32),
	//	simon->getHeight()
	//	);

	//Box boxphase = Collision::GetSweptBroadphaseBox(box);
	//if (Collision::AABBCheck(boxphase, block))
	//{
	//	if (simon->getObjState() == MS_NORMAL)
	//	{
	//		int blood = simon->getBlood();
	//		blood--;
	//		simon->setBlood(blood);
	//		if (simon->getBlood() == 0)
	//		{
	//			simon->changeState(MS_DIE);		//simon die
	//		}
	//		else
	//		{
	//			simon->changeState(MS_PROTECT);	//simon 
	//		}
	//	}
	//}

	/*if(veloc_x > 0)
	{
		turnRight();
	}
	else
	{
		turnLeft();
	}*/

	pos_x_old = pos_x;
	pos_y_old = pos_y;

	if (pos_x - STONE_KNIGHT_DISTANCE_X >= center_pos_x)
	{
		turnLeft();
	}
	else if (pos_x + STONE_KNIGHT_DISTANCE_X <= center_pos_x)
	{
		turnRight();
	}

	if (veloc_x >= 0)
	{
		direction = 2;
	} 
	else
	{
		direction = 1;
	}
}

void StoneKnight::changeState(int state)
{
	obj_state = state;
	switch (obj_state)
	{
	case ES_JUMP:
		veloc_y = STONE_KNIGHT_VY;
		accel_y = STONE_KNIGHT_GRAVITY;
		break;

	case ES_ACTIVING:
		count_time = 0;
		if (is_turn_left == true)
		{
			turnLeft();
		}
		else
		{
			turnRight();
		}
		accel_y = STONE_KNIGHT_GRAVITY;
		break;

	case ES_PAUSE:
		timeDie = 0;
		break;

	case ES_DIED:
		timeDie = 0;
		blood--;
		if (blood > 0)
		{
			changeState(ES_PAUSE);
		}
		else
		{
			p_sprite = p_light;
			veloc_x = 0;
		}		
		break;
	}
}
